home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: malloc question
- Date: 10 Mar 1996 10:18:06 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4hv6cuINNpi0@keats.ugrad.cs.ubc.ca>
- References: <4htonk$350@news.hklink.net> <4hv45e$svm@inet-nntp-gw-1.us.oracle.com>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4hv45e$svm@inet-nntp-gw-1.us.oracle.com>,
- William Kaufman <wkaufman@wkaufman.us.oracle.com> wrote:
- >In article <4htonk$350@news.hklink.net> alex@station.net (Alex Chu) writes:
- >]
- >] main()
- >] {
- >] PITEM head, current;
- >] head=(PITEM) malloc(sizeof(ITEM));
- >] ^^^^^^^
- >] head->val=1;
- >] }
- >]
- >] I want to know why need to use the type casting PITEM in front of the
- >] malloc ? Please help!
- >
- > Either:
- >
- > a) you're using a pre-ANSI compiler which complains that you can't
- >portably cast from (char *) to (PITEM) (in which case, you could go and
- >get an ANSI compiler); or,
- >
- > b) you aren't including <stdlib.h>, where malloc() is defined as
- >returning (void *), and the compiler thinks it returns (int).
-
- Which is a serious error that summons undefined behavior. Never mind that the
- mapping function from integral types to pointers is implementation defined;
- never mind that there is a possible disparity between the size of an int and a
- pointer---these issues don't even enter into it. An int return value may not
- even be passed back _in the same register_ as a pointer return value! This is
- simply a case of calling a function that is declared one way, and defined
- another way, cast or no cast.
-
- If I were implementing a C compiler for, say, a 680x0 architecture, I might use
- the A0 register for returning pointers, and D0 for returning integers, just
- because it is more convenient for the caller to have a pointer ready in an
- address register and an integer ready in a data register.
-
- The fact that a function call which has an incompatible declaration (implicit
- or otherwise) with respect to the function definition is undefiend would allow
- me that implementational latitude.
- --
-
-